﻿using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Edu.Http.Extensions;
using Edu.Http.Proxy;
using Infrastructure.Config.Core.External;
using Infrastructure.ExternalService.Core.FundSystem;
using Infrastructure.ExternalService.Core.Imp.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Infrastructure.ExternalService.Core.Imp.FundSystem
{
    public class FundSystemExternal : BaseHttpClient, IFundSystemExternal
    {
        private readonly OrderCenterApiOptions _orderingOptions;
        private readonly string formUrlencoded = "application/x-www-form-urlencoded";

        public FundSystemExternal(IOptions<OrderCenterApiOptions> options,
            HttpClient client, ILoggerFactory loggerFactory)
            : base(client, loggerFactory, options.Value.FundSystemOptions.EndPoint)
        {
            _orderingOptions = options.Value
                ?? throw new HttpProxyException($"{nameof(FundSystemExternal)}配置获取失败");

            loggerFactory.CreateLogger<FundSystemExternal>();
        }

        /// <summary>
        /// 设置BaseHttpClient的BaseAddress：该值默认是从构造函数传入的，但重写该方法可以改变该值
        /// </summary>
        /// <returns></returns>
        protected override string SetHttpClientBaseAddress()
        {
            return null;
        }

        /// <summary>
        /// 电子发票状态查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<InvoiceInfoList> GetInvoiceInfoAsync(GetInvoiceRequest request)
        {
            var formStr = ConvertRequestToCommonFormUrlEncode(request);
            var response = await PostJsonAsync<Responses.WrapResponse<InvoiceInfoList>>(url: _orderingOptions.FundSystemOptions.GetInvoiceInfo, jsonStr: formStr, contentType: formUrlencoded, headers: null);

            if (response != null && response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
            {
                return response.Data;
            }
            throw new HttpProxyException($"{response.Msg ?? "网络繁忙，电子发票状态查询失败，请稍后重试！"}");
        }

        /// <summary>
        /// 学员账户支付校验
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ValidatePaymentResponse> ValidatePaymentAsync(ValidatePaymentRequest request)
        {
            var formStr = ConvertRequestToCommonFormUrlEncode(request);
            var response = await PostJsonAsync<Responses.WrapResponse<ValidatePaymentResponse>>(url: _orderingOptions.FundSystemOptions.ValidatePayment, jsonStr: formStr, contentType: formUrlencoded, headers: null);

            if (response != null && response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
            {
                return response.Data;
            }
            throw new HttpProxyException($"{response.Msg ?? "网络繁忙，账户支付校验失败，请稍后重试！"}");
        }

        /// <summary>
        /// 学员账户支付
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<MakePaymentResponse> MakePaymentAsync(MakePaymentRequest request)
        {
            var formStr = ConvertRequestToCommonFormUrlEncode(request);
            var response = await PostJsonAsync<Responses.WrapResponse<MakePaymentResponse>>(url: _orderingOptions.FundSystemOptions.MakePayment, jsonStr: formStr, contentType: formUrlencoded, headers: null);

            if (response != null && response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
            {
                return response.Data;
            }
            throw new HttpProxyException($"{response.Msg ?? "网络繁忙，支付账户获取失败，请稍后重试！"}");
        }

        /// <summary>
        /// 通过订单流水号获取支付总金额
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<List<PaymentBalance>> GetPaymentAmountAsync(GetPaymentAmountRequest request)
        {
            var formStr = ConvertRequestToCommonFormUrlEncode(request);
            var response = await PostJsonAsync<Responses.WrapResponse<List<PaymentBalance>>>(url: _orderingOptions.FundSystemOptions.GetPaymentBalance, jsonStr: formStr, contentType: formUrlencoded, headers: null);

            if (response != null && response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
            {
                return response.Data;
            }
            throw new HttpProxyException($"{response.Msg ?? "网络繁忙，查验交易流水失败，请稍后重试！"}");
        }
    }
}
